//
//  AudioTool.m
//  QQMusic
//
//  Created by wwj on 16/4/21.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "AudioTool.h"

static NSMutableDictionary *_playerDictary;


@implementation AudioTool
+(void)initialize{
    _playerDictary = [NSMutableDictionary dictionary];
}

+ (AVAudioPlayer *)playMusicWithName:(NSString *)name{
    AVAudioPlayer *player = [_playerDictary objectForKey:name];
    if (player == nil) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:nil];
        if (url == nil) return nil;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [_playerDictary setObject:player forKey:name];
        [player prepareToPlay];
    }
    [player play];
    return player;
}

+ (AVAudioPlayer *)pauseMusicWithName:(NSString *)name{
    AVAudioPlayer *player = _playerDictary[name];
    if (player) {
        [player pause];
    }
    return player;
}

+ (void)stopMusicWithName:(NSString *)name{
    AVAudioPlayer *player = _playerDictary[name];
    if (player) {
        [player stop];
        [_playerDictary removeObjectForKey:player];
        player = nil;
    }
}
@end
